package leetcode.dp;

/**
 * @author mazouri
 * @create 2021-09-17 11:53
 */
public class _1143_longestCommonSubsequence {
    public int longestCommonSubsequence(String text1, String text2) {
        if (text1 == null || text1.length() == 0 || text2 == null || text2.length() == 0) return 0;

        int row = text1.length() + 1;
        int col = text2.length() + 1;

        int[][] dp = new int[row][col];

        for (int i = 0; i < row; i++) {
            dp[i][0] = 0;
        }

        for (int i = 0; i < dp[0].length; i++) {
            dp[0][i] = 0;
        }

        for (int i = 1; i < row; i++) {
            for (int j = 1; j < col; j++) {
                if (text1.charAt(i-1) == text2.charAt(j-1)) dp[i][j] = dp[i - 1][j - 1] + 1;
                else dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
            }
        }

        return dp[row-1][col-1];
    }
}
